package snsdbook.servlets.likes;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.SQLUtil;
import util.Util;

/**
 * Servlet implementation class ILikeQuery
 */
public class ILikeQuery extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ILikeQuery() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String user = request.getParameter("username");
    String type = request.getParameter("type");
    String id = request.getParameter("id");
    String query = null;
    
	  if (type.equals("likes")) {
	    query = "SELECT COUNT(id_likes) AS cnt FROM likes " +
	            "WHERE id_berita='" + id + "' AND " +
	            "username='" + user + "'";
	  } else if (type.equals("dislike")) {
	    query = "SELECT COUNT(id_dislike) AS cnt FROM dislike " +
	            "WHERE id_berita='" + id + "' AND " +
	            "username='" + user + "'";
	  } else {
	    Util.PrintAndClose(response, "UNRECOGNIZED TYPE PARAMETER");
	  }

	  String[] answer = SQLUtil.Query(query);

	  if (Integer.valueOf(answer[0]) > 0) {
	    Util.PrintAndClose(response, "YES");
	  } else {
      Util.PrintAndClose(response, "NO");
	  }
	}

}
